package com.wangxiang.shop.product.service.impl;


import cn.hutool.core.util.RandomUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangxiang.common.utils.*;
import com.wangxiang.shop.product.dao.ShopTypeDao;
import com.wangxiang.shop.product.dao.SkuDao;
import com.wangxiang.shop.product.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wangxiang.shop.product.dao.SpuDao;
import com.wangxiang.shop.product.service.SpuService;


@Service("spuService")
public class SpuServiceImpl extends ServiceImpl<SpuDao, SpuEntity> implements SpuService {
    @Autowired
    private SkuDao skuDao;
    @Autowired
    private SpuDao spuDao;
    @Autowired
    private ShopTypeDao shopTypeDao;

    @Override
    public List<MemberCartEntity> getCart(List<ShopCartEntity>cartEntity) {
        List<MemberCartEntity> memberCartEntities = new ArrayList<>();
        for (ShopCartEntity shopCartEntity : cartEntity) {
            MemberCartEntity memberCartEntity = new MemberCartEntity();
            memberCartEntity.setCartId(shopCartEntity.getCartId());
            memberCartEntity.setUserId(shopCartEntity.getUserId());
            memberCartEntity.setSpuId(shopCartEntity.getSpuId());
            memberCartEntity.setSkuId(shopCartEntity.getSkuId());
            memberCartEntity.setNum(shopCartEntity.getNum());

            SpuEntity spuEntity = spuDao.selectById(shopCartEntity.getSpuId());
            if (spuEntity!=null){
                memberCartEntity.setImage(spuEntity.getImage());
                memberCartEntity.setName(spuEntity.getName());
                memberCartEntity.setStore(spuEntity.getStore());
                memberCartEntity.setPrice(spuEntity.getPrice());
//                System.out.println("价格"+spuEntity.getPrice());
            }
            if (shopCartEntity.getSkuId()!=0){
                SkuEntity skuEntity = skuDao.selectById(shopCartEntity.getSkuId());
                memberCartEntity.setPrice(skuEntity.getPrice());
                memberCartEntity.setShopStatus(skuEntity.getStatus());
                memberCartEntity.setSize(skuEntity.getSize());
                memberCartEntity.setStyle(skuEntity.getStyle());
                memberCartEntity.setSkuImage(skuEntity.getImage());
            }
            memberCartEntities.add(memberCartEntity);

        }
        System.out.println("会员服务调用服务层"+memberCartEntities);
        return memberCartEntities;
    }

    @Override
    public PageEntity page(PageEntity pageEntity) {
        //调用mybatis分页工具
        PageHelper.startPage(pageEntity.getPageNum(), pageEntity.getPageSize());
        List<SpuEntity> list = spuDao.selectList(null);
        PageInfo<SpuEntity> spuEntityPageInfo = new PageInfo<>(list);
        PageGenerate pageGenerate = new PageGenerate();
        PageEntity generate = pageGenerate.generate(spuEntityPageInfo);
        return generate;
    }

    @Override
    public List<SpuEntity> getSpuByType(String type) {
        //新版本使用索引
        int id = shopTypeDao.getTypeIdByTypeName(type);
        List<SpuEntity> list = spuDao.getEntityListByIndex(id);
//        List<SpuEntity> list = spuDao.selectByType(type);

        return list;
    }

    @Override
    public List<SpuEntity> search(String search) {
        StrUtil strUtil = new StrUtil();
        String s = strUtil.likeStr(search);
        List<SpuEntity> list = spuDao.searchByTitel(s);
        ArrayList<SpuEntity> list1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            int c = RandomUtil.randomInt(0, list1.size()+1);
            if (!list1.contains(list.get(i))){
                if (list1.size()<100){
                    list1.add(list.get(c));
                }
            }
        }



        return list1;
    }

    @Override
    public List<SpuEntity> searchById(int spuId) {
        ArrayList<SpuEntity> list = new ArrayList<>();
        SpuEntity spuEntity = spuDao.selectById(spuId);
        if (spuEntity!=null){
            list.add(spuEntity);
            return list;
        }else {
            return null;
        }

    }

    @Override
    public int updateStatus(int id, int status) {
        SpuEntity spuEntity = spuDao.selectById(id);
        MyStringUtile myStringUtile = new MyStringUtile();
        ArrayList<Integer> skuId = myStringUtile.getSkuArray(spuEntity.getSkuId());
        int i = spuDao.updateStatus(id, status);
        ArrayList<Integer> integers = new ArrayList<>();
        if (i>0){
            for (Integer integer : skuId) {
                int i1 = skuDao.updateStatus(integer, status);
                if (i1>0){
                    integers.add(integer);
                }
            }
        }
        if (integers.size()>0){
            return 1;
        }else {
            return 0;
        }

    }

    @Override
    public List<HashMap<String, Object>> param(int id) {
        List<HashMap<String, Object>> list = new ArrayList<>();
        SpuEntity spuEntity = spuDao.selectById(id);
        StrChangeMapUtil strChangeMapUtil = new StrChangeMapUtil();
        HashMap<String, Object> map = strChangeMapUtil.map(spuEntity.getParam());
        if (spuEntity.getParam()!=null){
            map.forEach(new BiConsumer<String, Object>() {
                @Override
                public void accept(String s, Object o) {
                    HashMap<String, Object> mapKV = new HashMap<>();
                    mapKV.put(s,o);
                    list.add(mapKV);

                }
            });
        }


        return list;
    }

    @Override
    public List<SkuEntity> getWareList(int id) {
        SpuEntity spuEntity = spuDao.selectById(id);
        MyStringUtile myStringUtile = new MyStringUtile();
        ArrayList<Integer> skuArray = myStringUtile.getSkuArray(spuEntity.getSkuId());
        ArrayList<SkuEntity> skuEntities = new ArrayList<>();
        for (Integer skuId : skuArray) {
            SkuEntity skuEntity = skuDao.selectById(skuId);
            skuEntities.add(skuEntity);
        }
        return skuEntities;
    }

    @Override
    public List<CartEntity> memberCart(List<CartEntity> entity) {
        ArrayList<CartEntity> list = new ArrayList<>();
        entity.forEach((item)->{
            CartEntity cartEntity = new CartEntity();
            cartEntity.setCartId(item.getCartId());
            cartEntity.setUserName(item.getUserName());
            cartEntity.setSpuId(item.getSpuId());
            cartEntity.setSkuId(item.getSkuId());
            cartEntity.setUserId(item.getUserId());
            cartEntity.setNum(item.getNum());
            cartEntity.setAllPrice(item.getAllPrice());
            cartEntity.setUnitPrice(item.getUnitPrice());
            cartEntity.setStatus(item.getStatus());
            cartEntity.setTime(item.getTime());
            //商品组合
            SpuEntity spuEntity = spuDao.selectById(item.getSpuId());
            SkuEntity skuEntity = skuDao.selectById(item.getSkuId());

            cartEntity.setBrand(spuEntity.getBrand());
            cartEntity.setStore(spuEntity.getStore());
            cartEntity.setTitle(spuEntity.getTitle());
            cartEntity.setType(spuEntity.getType());
            if (skuEntity.getImage()!=null){
                cartEntity.setImage(skuEntity.getImage());
            }else {
                cartEntity.setImage(spuEntity.getImage());
            }
            //发售日期
            StrChangeMapUtil strChangeMapUtil = new StrChangeMapUtil();
            HashMap<String, Object> map = strChangeMapUtil.map(spuEntity.getParam());
            if (map.get("上市时间")!=null){
                cartEntity.setSellTime(map.get("上市时间").toString());
            }
            cartEntity.setSize(skuEntity.getSize());
            cartEntity.setStyle(skuEntity.getStyle());
            list.add(cartEntity);

        });




        return list;
    }

    @Override
    public List<HashMap<String, Object>> auditshopService(List<HashMap<String, Object>> maps) {
        for (HashMap<String, Object> map : maps) {
            int spuId = Integer.valueOf(map.get("spuId").toString());
            SpuEntity spuEntity = spuDao.selectById(spuId);
            map.put("shopType",spuEntity.getType());
            map.put("shopTitle",spuEntity.getTitle());
            map.put("spuImage",spuEntity.getImage());
            map.put("store",spuEntity.getStore());
            map.put("brand",spuEntity.getBrand());
            map.put("shopName",spuEntity.getName());
        }
        return maps;
    }

    @Override
    public List<HashMap<String, Object>> memberOrderList(List<HashMap<String, Object>> maps) {
        String nullStr = "";
        maps.forEach((item)->{
            int spuId = Integer.valueOf(item.get("spuId").toString());
            int skuId = Integer.valueOf(item.get("skuId").toString());
            SpuEntity spuEntity = spuDao.selectById(spuId);
            SkuEntity skuEntity = skuDao.selectById(skuId);
            item.put("shopTitle",spuEntity.getTitle());
            item.put("shopBrand",spuEntity.getBrand());
            item.put("shopStore",spuEntity.getStore());
            item.put("shopType",spuEntity.getType());
            item.put("shopStyle",skuEntity.getStyle());
            item.put("shopSize",skuEntity.getSize());
            if (skuEntity.getImage()==null|nullStr.equals(skuEntity.getImage())){
                item.put("shopImage",spuEntity.getImage());
            }else {
                item.put("shopImage",skuEntity.getImage());
            }

        });
        return maps;
    }

    @Override
    public int addShop(HashMap<String, Object> mapAll, ArrayList<HashMap<Object, Object>> paramListMap, ArrayList<HashMap<String, Object>> skuListMap) {
        SpuEntity spuEntity = new SpuEntity();
        spuEntity.setTitle(mapAll.get("shopTitle").toString());
        double price = Double.valueOf(skuListMap.get(0).get("price").toString());
        spuEntity.setPrice(price);
        int wareTotal = 0;
        for (HashMap<String, Object> map : skuListMap) {
            int ware = Integer.valueOf(map.get("ware").toString());
            wareTotal = ware+wareTotal;
        }
        //参数还有上市日期产地
        spuEntity.setWareTotal(wareTotal);
        HashMap<Object, Object> map = new HashMap<>();
        for (HashMap<Object, Object> objectObjectHashMap : paramListMap) {
          for (Map.Entry<Object, Object> objectMap : objectObjectHashMap.entrySet()){
            map.put(objectMap.getKey(),objectMap.getValue());
          }
        }
        map.put("上市时间",mapAll.get("shopTime").toString());
        map.put("商品产地",mapAll.get("shopLocation").toString());
        spuEntity.setParam(String.valueOf(map));

        spuEntity.setType(mapAll.get("shopType").toString());
        spuEntity.setBrand(mapAll.get("shopBrand").toString());
        spuEntity.setName(mapAll.get("shopName").toString());
        spuEntity.setImage(mapAll.get("shopImage").toString());
        spuEntity.setLocation(mapAll.get("shopLocation").toString());
        spuEntity.setStore(mapAll.get("shopStore").toString());
        spuEntity.setStatus(3); //新增审核
//        System.out.println("实体"+spuEntity);
        int i = spuDao.addGetKey(spuEntity);
        ArrayList<Integer> skuIdList = new ArrayList<>();
        if (i>0){
            //插入sku表
            for (HashMap<String, Object> skuEntityMap : skuListMap) {
                SkuEntity skuEntity = new SkuEntity();
                if (skuEntityMap.get("style")!=null){
                    skuEntity.setStyle(skuEntityMap.get("style").toString());
                }
                if (skuEntityMap.get("size")!=null){
                    skuEntity.setSize(skuEntityMap.get("size").toString());
                }
                double price1 = Double.valueOf(skuEntityMap.get("price").toString());
                skuEntity.setPrice(price1);
                int ware  = Integer.valueOf(skuEntityMap.get("ware").toString());
                skuEntity.setBrand(mapAll.get("shopBrand").toString());
                skuEntity.setImage(mapAll.get("shopImage").toString());
                skuEntity.setWare(ware);
                spuEntity.setSpuId(spuEntity.getId());
                skuEntity.setType(mapAll.get("shopType").toString());
                skuEntity.setStatus(3);
                int i1 = skuDao.addGetKey(skuEntity);
                if (i1>0){
                    skuIdList.add(skuEntity.getId());
                }else {
                    return 2;  //sku表插入失败
                }
            }

            //同步spu的skuid字段
            if (skuIdList.size()!=0){
                String s = String.valueOf(skuIdList);
                int i1 = spuDao.updataSkuId(spuEntity.getId(), s);
                if (i1>0){
                    return spuEntity.getId();
                }else {
                    return 3;
                }
            }


        }
        return i;


    }


}